// 创建store
import { createStore } from 'redux'
import milk from '../data/豆奶制品.json'
import veg from '../data/蔬菜类.json'
import sta from '../data/主食.json'

let state = {
    name: '2004',
    menu: ['主食', '蛋奶豆制品', '蔬菜类'],
    activeMenu: '主食',
    list: milk.concat(veg, sta), // 复杂数据类型、引用数据类型
    isAlertShow: false,
    rMenu: ['人气Top', '大师咖啡', '小鹿茶精选'],
    tempList:  [
        {
            name: '红巴士茶',
            price: 27,
            type: '人气Top',
        },
        {
            name: '模式茶',
            price: 27,
            type: '小鹿茶精选',
        },
        {
            name: '墨香茶',
            price: 30,
            type: '大师咖啡',
        },
        {
            name: '拿铁',
            price: 40,
            type: '大师咖啡',
        },
        {
            name: '瑞纳冰',
            price: 27,
            type: '人气Top',
        }
    ],
    rList: [
        {
            name: '红巴士茶',
            price: 27,
            type: '人气Top',
        },
        {
            name: '模式茶',
            price: 27,
            type: '小鹿茶精选',
        },
        {
            name: '墨香茶',
            price: 30,
            type: '大师咖啡',
        },
        {
            name: '拿铁',
            price: 40,
            type: '大师咖啡',
        },
        {
            name: '瑞纳冰',
            price: 27,
            type: '人气Top',
        }
    ]

}
// list = [123 ,345, 4,8] 1010100001001010101

// 接收一个旧的state，action，返回一个新的state
function reducer(state, action) {
    if(action.type === 'SET_NAME') {
        state.name = action.value
    } else if(action.type === 'SET_MENU') {
        state.activeMenu = action.value
    } else if(action.type === 'SET_FOOD') {
        // 设置食物是否被选中
        let list = state.list
        for(let i = 0; i< list.length; i++) {
            if(list[i].name === action.value.name) {
                if(list[i].isSelected) {
                    list[i].isSelected = 0
                } else {
                    list[i].isSelected = 1
                }
                
            }
        }
        state.list = list
    } else if(action.type === 'SET_ALERTSHOW') {
        state.isAlertShow = false
    } else if(action.type === 'ADD') {
        // 加入购物车
        for(let i =0; i< state.rList.length; i++) {
            if(state.rList[i].name === action.name) {
                // 找到商品了
                if(state.rList[i].num) { // 有num。非第一次加入购物车
                    state.rList[i].num++
                } else {
                    // 没有num字段或者num为0
                    state.rList[i].num = 1
                }
            }
        }
    } else if(action.type === 'MINUS') {
        for(let i =0; i< state.rList.length; i++) {
            if(state.rList[i].name === action.name) {
                // 找到商品了
                if(state.rList[i].num) { // 有num。非第一次加入购物车
                    state.rList[i].num--
                }
            }
        }
    }
    return JSON.parse(JSON.stringify(state))
}


const store = createStore(reducer, state)
export default store